<template>
  <el-container>
    <el-header><Header></Header></el-header>
    <el-container>
      <el-aside style="width: 15%;background-color: #ffffff;">
           <el-menu v-show="isAdmin" default-active="2" class="el-menu-vertical-demo" v-for="(goodType, index) in typeList" :key="goodType.typeId">
             <el-menu-item>
               <el-button style="color: palevioletred;" type="text">{{ goodType.goodsType }}({{ goodType.sum }})</el-button>
             </el-menu-item>
           </el-menu>
      </el-aside>
       <div class="home-box">
         <el-row :gutter="10">
           <el-col :xs="24" :sm="24" :md="10" :lg="10" :offset="7">
             <div v-show="isAdmin">
               <h1 class="register-title" style="color: coral;">上传资料</h1>
               <el-form status-icon label-width="100px" class="demo-ruleForm">
                 <el-form-item label="资料名" prop="goodName"><el-input type="text" v-model="goodName" placeholder="输入资料名"></el-input></el-form-item>
                 <el-form-item label="价格" prop="price"><el-input type="text" v-model="price" placeholder="输入价格"></el-input></el-form-item>
                 <el-form-item label="数量" prop="count"><el-input type="text" v-model="count" placeholder="输入数量"></el-input></el-form-item>
                 <el-form-item label="类型" >
                  <el-select v-model="value" placeholder="选择类型" >
                     <el-option v-for="goodType in typeList" :key="goodType.typeId" :label="goodType.goodsType" :value="goodType.typeId"></el-option>
                  </el-select>
                 </el-form-item>
                 <el-form-item label="描述"><el-input type="textarea" maxlength="40" show-word-limit v-model="message"></el-input></el-form-item>
                 <el-form-item label="资料图片">
                   <el-upload class="upload-demo" :action="UploadUrl()" :before-upload="beforeAvatarUpload">
                     <el-button size="small" type="warning" icon="el-icon-upload2">上传</el-button>
                   </el-upload>
                 </el-form-item>
                 <el-form-item>
                   <div style="margin-top: 25px;">
                     <el-button style="float: left;" type="primary" size="small" @click="upload()">确认创建</el-button>
                     <el-button style="float: left;" size="small" type="danger" icon="el-icon-delete" @click="del()">取消</el-button>
                   </div>
                 </el-form-item>
               </el-form>
             </div>
           </el-col>
         </el-row>
       </div>
      </el-container>
    <el-footer><Footer></Footer></el-footer>
  </el-container>
</template>

<script>
import Footer from '@/components/Footer';
import Header from '@/components/Header';
export default {
  data() {
    return {
      isAdmin: this.$store.getters.getUser().isAdmin,
      value: null,
      typeList: null,
      url: 'https://jsonplaceholder.typicode.com/posts/',
      message: '',
      price: '',
      count: '',
      goodName: '',
      goodPhoto: null
    };
  },
  // 组件声明
  components: {
    Footer: Footer,
    Header: Header
  },
  created() {
    this.findAll()

  },

  methods: {
    findAll() {
      let _this = this;
      this.axios
        .get(this.apiUrl5 + '/good/findAllType')
        .then(res => {
          (_this.typeList = res.data), (_this.value = res.data[0].typeId);
        })
        .catch(err => {
          console.log(err);
        });
    },

    beforeAvatarUpload(file) {
      this.goodPhoto = file;
    },

    upload() {
      //文件上传时 请求方式必须是post  enctype必须为multipart/form-data
      var formData = new FormData();

      formData.append('goodPhoto', this.goodPhoto);
      formData.append(' price', this.price);
      formData.append('count', this.count);
      formData.append('message', this.message);
      formData.append('typeId', this.value);
      formData.append('goodName', this.goodName);

      let _this = this;
      this.axios({
        method: 'post',
        url: _this.apiUrl5 + '/good/upload',
        data: formData,
        headers: {
          'content-type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.state) {
          _this.findAll(),
          _this.$message({
            message: res.data.msg,
            type: 'success'
          });
          _this.$router.push('/home');
        } else {
          _this.$message({
            message: res.data.msg,
            type: 'warning'
          });
        }
      });
    },
    del() {
      this.$router.push('/home');
    },
    UploadUrl() {
      return this.url;
    }
  }
};
</script>

<style>
.home-box {
  width: 100%;
  padding: 2px;
  float: left;
 /* float: right; */
  /*  margin: 100px auto; */
  padding: 20px 50px 10px 10px;
  border: 1px solid #ffffff;
  /* background-image: url(../../static/zm1.jpg); */
}
.el-header,
.el-footer {
  /*  background-color: #B3C0D1; */
  /* background-image: url(../../static/zm6.jpg); */
  /*  color: #333; */
  /* text-align: center; */
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  line-height: 50px;
}

.el-aside {
  color: #333;
  text-align: center;
   line-height: 200px;
}

.el-main {
  color: #333;
  text-align: center;
  height: 800px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  /*  background: #99a9bf; */
}
.bg-purple {
  /* background: #d3dce6; */
}
.bg-purple-light {
  /*  background: #e5e9f2; */
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 5px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  /* clear: both */
}

.box-card {
  width: 350px;
  margin-top: 20px;
}
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
